package com.ohxwj.modules.web.controller;

import com.ohxwj.common.annotation.SysLog;
import com.ohxwj.common.utils.R;
import com.ohxwj.modules.web.entity.WebMenuEntity;
import com.ohxwj.modules.web.service.WebArticleService;
import com.ohxwj.modules.web.service.WebMenuService;
import org.apache.http.HttpStatus;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2018-04-16.
 */
@RestController
@RequestMapping("/web/menu")
public class WebMenuController extends AbstractAction {
    @Autowired
    private WebMenuService webMenuService;
    @Autowired
    private WebArticleService webArticleService;

    public static final int MAX_MENU = 6;

    @SysLog("add web menu")
    @RequestMapping("/add")
    @RequiresPermissions("web:menu:save")
    public R add(@RequestBody WebMenuEntity webMenuEntity) {
        if (webMenuEntity.getParentId() == 0) {
            List<WebMenuEntity> webMenuEntities = webMenuService.queryByParentId(0);
            if (null != webMenuEntities && webMenuEntities.size() >= MAX_MENU ) {
                return R.error("一级栏目数目不能大于" + MAX_MENU);
            }
        }
        WebMenuEntity parent = webMenuService.loadWebMenuById(webMenuEntity.getParentId());
        webMenuEntity.setHeight(null == parent ? 0 : parent.getHeight() + 1);
        webMenuService.insertMenu(webMenuEntity);

        return R.ok();
    }

    @SysLog("update web menu")
    @RequestMapping("/update")
    @RequiresPermissions("web:menu:update")
    public R update(@RequestBody WebMenuEntity webMenuEntity) {
        WebMenuEntity oldMenu = webMenuService.loadWebMenuById(webMenuEntity.getId());
        if (oldMenu.getType() != webMenuEntity.getType()) {
            Map<String, Object> params = new HashMap<>();
            params.put("limit", 1000);
            params.put("offset", 0);
            params.put("page", 1);
            params.put("menuId", webMenuEntity.getId());
            int total = webArticleService.queryTotal(params);
            if (total > 0) {
                return R.error("该栏目已存在文章，请先删除文章再修改栏目类型");
            }
        }
        webMenuService.updateMenu(webMenuEntity);

        return R.ok();
    }

    @RequestMapping("/list")
    public R list(HttpServletRequest request) {
        int menuId = getIntegerParamFromRequest(request, "menuId");
        WebMenuEntity webMenuEntity = webMenuService.loadWebMenuById(menuId);
        List<WebMenuEntity> webMenuEntities = webMenuService.queryMenus();
        if (null != webMenuEntity && webMenuEntity.getHeight() > 0) {
            int parentId = getRootId(webMenuEntity);
            webMenuEntities = webMenuService.queryByParentId(parentId);
        }
        return R.ok().put("data", webMenuEntities);
    }



    @SysLog("delete menu")
    @RequestMapping("/delete")
    @RequiresPermissions("web:menu:delete")
    public R delete(int id) {

        WebMenuEntity webMenuEntity = webMenuService.loadWebMenuById(id);
        if (null == webMenuEntity) {
            return R.error(HttpStatus.SC_NOT_FOUND, "栏目不存在");
        }
        List<WebMenuEntity> menuEntities = new ArrayList<>();
        switch (webMenuEntity.getHeight()) {
            case 0 :
                menuEntities = webMenuService.queryByParentId(id);
                break;
            case 1:
                menuEntities = webMenuService.queryNextMenuByParentId(id);
                break;
        }
        if (null != menuEntities) {
            for (WebMenuEntity menuEntity : menuEntities) {
                webMenuService.deleteMenu(menuEntity.getId());
            }
        }
        webMenuService.deleteMenu(id);

        return R.ok();
    }

    private int getRootId(WebMenuEntity webMenuEntity) {
        int menuId = 0;
        do {
            menuId = webMenuEntity.getId();
            webMenuEntity = webMenuService.loadWebMenuById(webMenuEntity.getParentId());
        } while (null != webMenuEntity);
        return menuId;
    }
}
